from django.conf.urls.defaults import *
from django.contrib.syndication.feeds import Feed

from metaweb import perform_read
from models import Financing, FinancingTwitter
import sys
import json_custom as json

class VCFinancing(Feed):
    title = "BioVenturist - VC Financings"
    link = "/here/"
    description = "A listing of recent biotechnology venture capital financings."

    def items(self):
		html = perform_read('vcfinancings.mql', {},False,);
		obj = json.read(html);
		q1 = obj["q1"];
		result = q1["result"];
		
		fList = [];
		for x in result:
			test = 0;
				
			f = Financing();
			
			if x['company_invested'] is not None:
				company_invested=x['company_invested'][0]
                
				f.company_invested = company_invested['name']
				f.company_invested_id = company_invested['id']
                
				t_s = ''
				for i,a in enumerate(company_invested['area_of_expertise']):
					t_s = t_s + a;
					if i<len(company_invested['area_of_expertise'])-1:
						t_s = t_s + ', ';
				f.technologies = t_s

				d_s = ''
				for i,a in enumerate(company_invested['develops_treatments_for_disease']):
					d_s = d_s + a
					if i < len(company_invested['develops_treatments_for_disease'])-1:
						d_s = d_s + ', '
				f.medical_focuses = d_s				

			
			if x['date'] is not None:
				f.date=x['date']
			
			if x['currency'] is not None:
				f.currency=x['currency']
							
			if x['amount_raised'] is not None:
				f.amount_raised = x['amount_raised']/1000000
			
			if x['investment_round'] is not None:
				f.investment_round = x['investment_round']
			
			if x['investors'] is not None:
				s = '';
				for i,a in enumerate(x['investors']):
					s = s + a;
					if i<len(x['investors'])-1:
						s = s + ', ';
					f.investors = s;

			fList.append(f);
		
		
		return fList
        
class VCFinancingTwitter(VCFinancing):
    
    def items(self):
        html = perform_read('vcfinancings.mql', {},True,);
        obj = json.read(html);
        q1 = obj["q1"];
        result = q1["result"];
        
        fList = [];
        for x in result:
            test = 0;
                
            f = FinancingTwitter();
            
            if x['company_invested'] is not None:
                company_invested = x['company_invested'][0]
                f.company_invested_id = company_invested['id']
                f.company_invested = company_invested['name']
                t_s = '';
                for i,a in enumerate(company_invested['area_of_expertise']):
					t_s = t_s + a;
					if i<len(company_invested['area_of_expertise'])-1:
						t_s = t_s + ', ';
                f.technologies = t_s;
                d_s = '';
                for i,a in enumerate(company_invested['develops_treatments_for_disease']):
					d_s = d_s + a;
					if i < len(company_invested['develops_treatments_for_disease'])-1:
						d_s = d_s + ', ';
                f.medical_focuses = d_s;				
							
            if x['date'] is not None:
                f.date=x['date']
            
            if x['currency'] is not None:
                f.currency=x['currency']
				                                    
            if x['amount_raised'] is not None:
                f.amount_raised = x['amount_raised']/1000000
            
            if x['investment_round'] is not None:
                f.investment_round = x['investment_round']
            
            if x['investors'] is not None:
				s = '';
				for i,a in enumerate(x['investors']):
					s = s + a;
					if i<len(x['investors'])-1:
						s = s + ', ';
				f.investors = s;
			
            if x['web_reference'] is not None:
                s = '';
                for a in x['web_reference']:
                    f.web_ref = a; # right now only link to the last reference

            fList.append(f);
        
        
        return fList
